package team.seekers.hula.system.phoneMessage.controller;


import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import team.seekers.hula.common.annotation.ApiResponseBody;
import team.seekers.hula.common.entity.UserSessionDTO;
import team.seekers.hula.common.entity.enums.LoginTypeEnum;
import team.seekers.hula.common.entity.result.ApiExceptionEnum;
import team.seekers.hula.common.entity.result.ApiException;
import team.seekers.hula.common.utils.CaptchaUtils;
import team.seekers.hula.common.utils.JwtUtil;
import team.seekers.hula.common.utils.RedisConstants;
import team.seekers.hula.common.utils.RedisUtils;
import team.seekers.hula.system.phoneMessage.service.PhoneMessageService;
import team.seekers.hula.user.user.service.UserService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author hbc
 * @since 2021-02-15
 */
@Controller
@ApiResponseBody
@RequestMapping("/phone-message")
public class PhoneMessageController {

    @Autowired
    private PhoneMessageService phoneMessageService;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private UserService userService;

    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 给手机或邮箱发送验证码
     * @return
     */
    @GetMapping("/send")
    @ApiResponseBody
    public Void sendVerifyMessage(@RequestParam("tel") String tel,
                                  @RequestParam("email") String email) {
        String randomStr = CaptchaUtils.randomCode();
        redisUtils.set(RedisConstants.getCaptchaKey(tel), randomStr, RedisConstants.CAPTCHA_EXPIRE);

        if(StringUtils.isNotBlank(email)){

        }else if(StringUtils.isNotBlank(tel)){
            ArrayList<String> params = new ArrayList<>();
            params.add(randomStr);
            phoneMessageService.sendMessage(tel,params);
        }else {
            throw new ApiException(ApiExceptionEnum.PARAM_NOT_VALID,"手机号和邮箱全为空");
        }
        return null;
    }

    /**
     * 验证手机和验证码
     * @return
     */
    @GetMapping("/verify")
    @ApiResponseBody
    public String verifyCaptcha(@RequestParam("tel") String tel,
                                @RequestParam("unionId") String unionId,
                                @RequestParam(value = "miniOpenId",required = false) String miniOpenId,
                                  @RequestParam("code") String code) {
        phoneMessageService.verifyCaptcha(tel,code);
        if(userService.isExistPhone(tel)){
            userService.bindWxAndPhone(tel,unionId);
            if(StringUtils.isNotBlank(miniOpenId)){
                userService.checkAndBindOpenId(unionId,miniOpenId, LoginTypeEnum.MINI_APP);
            }
            UserSessionDTO userDO = userService.getUserSessionByTel(tel);
            return jwtUtil.getAndCacheJwtToken(userDO);
        }else{
            return null;
        }
    }
}

